#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>

using namespace std;

struct Node
{
    int x, y, id;
};

bool operator < ( const Node &a, const Node &b ) { return a.y < b.y; }

__int64 c[ 30001 ], ans[ 100001 ];
int n, m, right[ 30001 ], a[ 30001 ];
Node q[ 200001 ];
map<int, int> hash;

__int64 lowbit( __int64 t ) { return t & ( t ^ ( t - 1 ) ); }

__int64 sum( int p )
{
    __int64 ret = 0;
    while ( p >= 1 )
    {
        ret += c[ p ];
        p -= lowbit( p );
    }
    return ret;
}

void add( int p, int x )
{
    while ( p <= n )
    {
        c[ p ] += x;
        p += lowbit( p );
    }
}

void init( )
{
    int i, p = 1;
    memset( c, 0, sizeof( c ) );
    memset( right, 0, sizeof( right ) );
    scanf("%d", &n);
    hash.clear( );
    for ( i = 1; i <= n; i++ )
    {
        scanf("%d", &a[ i ]);
        if ( !hash[ a[ i ] ] )
            hash[ a[ i ] ] = p++;
    }
    scanf("%d", &m);
    for ( i = 0; i < m; i++ )
    {
        scanf("%d%d", &q[ i ].x, &q[ i ].y);
        q[ i ].id = i;
    }
    sort( q, q + m );
}

void work( )
{
    int i, j = 1;
    for ( i = 0; i < m; i++ )
    {
        while ( j <= q[ i ].y )
        {
            if ( !right[ hash[ a[ j ] ] ] )
            {
                right[ hash[ a[ j ] ] ] = j;
                add( j, a[ j ] );
            }
            else
            {
                add( right[ hash[ a[ j ] ] ], -a[ j ] );
                add( j, a[ j ] );
                right[ hash[ a[ j ] ] ] = j;
            } 
            j++;
        }
        if ( q[ i ].x == 1 )
            ans[ q[ i ].id ] = sum( q[ i ].y );
        else
            ans[ q[ i ].id ] = sum( q[ i ].y ) - sum( q[ i ].x - 1 );
    }
}

void print( )
{
    int i;
    for ( i = 0; i < m; i++ )
        printf("%I64d\n", ans[ i ]);
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        work( );
        print( );
    }
    return 0;
}
